Ext.onReady(function(){

    var tree=new Ext.tree.TreePanel({
        el:'tree-div',
        //useArrows:true,
        autoScroll:true,
        animate:false,
        rootVisible: false,
        containerScroll: true, 
        loader:new Ext.tree.TreeLoader({
            dataUrl:'../hf/treeMenu.do?action=menu'
        })
    });
    
    
    new Ext.tree.TreeSorter(tree, {folderSort:true});
    
    new Ext.tree.TreeEditor(tree,{
        allowBlank    : false
    });
    
    var root=new Ext.tree.AsyncTreeNode({
        text:'woshiroot',
        id:'0'
    });
    
    
    tree.setRootNode(root);
    tree.render();
    root.expand();
    
    var shortcuts = tree.getTreeEl();
    var contextmenu=new Ext.menu.Menu({
        id:'theContextMenu',
        items:[{
            text:'aaa',
            handler:function(){
                alert('asdfdss');
            }
        }]
    });
    tree.on('contextmenu',function(node,e){
        e.preventDefault();                 //禁止默认菜单弹出
        node.select();                      //选择当前节点
        contextmenu.showAt(e.getXY());
    });

    shortcuts.on('contextmenu',function(e){
        e.preventDefault();                 //禁止默认菜单弹出
        contextmenu.showAt(e.getXY());
    });
    
    tree.on('expandnode',function(node){
        Ext.log(node+'展开了');
    });
    
    tree.on('collapsenode',function(node){
        Ext.log(node+'折叠了');
    });
    
    /*-----------------------------------------------*/
    var myTreeLoader=new Ext.tree.TreeLoader({
            dataUrl:'../hf/treeMenu.do?action=checkboxMenu'
    });
    
    var tree2=new Ext.tree.TreePanel({
        el:'tree2',
        animate:true,
        enableDD:true,
        autoScroll:true,
        useArrows:true,
        containerScroll:true,
        dropConfig: {appendOnly:true},
        loader:myTreeLoader
    });
    
    myTreeLoader.on("beforeload", function(treeLoader, node) {
        treeLoader.baseParams.check = node.attributes.checked;
    }, this);
    
    new Ext.tree.TreeSorter(tree2,{folderSort:true});
    
    var root2=new Ext.tree.AsyncTreeNode({
        text:'my files',
        draggable:false,
        checked:true,
        id:'0'
    });
    
    tree2.setRootNode(root2);
    tree2.render();
       
    root2.expand();
    
    tree2.on({
        'checkchange':function(node,checked){

            var parentNode=node.parentNode;
            
            
            if(checked){
                /**
                 * 节点为真时，此节点的子节点,判断此节点的父节点时，判断父节点的子节点是否全部为
                 * 真，如果全部为真，则此父节点为真，如果不为真则不变
                 * 全部为真
                 */
                var childNodes=node.childNodes;
                
                for(var i=0;i<childNodes.length;i++){
                    var childNode=childNodes[i];
                    if(!childNode.attributes.checked){
                        childNode.ui.toggleCheck();
                    }
                }
                /**
                 * 此如果此节点有父节点，则判断此父节点的子节点是否全为真
                 * 如果全为真则此父节点也为真
                 */     
                if(parentNode&&!parentNode.attributes.checked){
                    var nodes=parentNode.childNodes
                    for(var i=0;i<nodes.length;i++){
                        /**
                         * 当一个父节点的子节点中的一个节点为false是，此复节点应为false
                         */
                        if(!nodes[i].attributes.checked){   
                            /**
                             * 如果此父节点为真时，改变父节点的状态
                             */
                            if(parentNode.attributes.checked){
                                parentNode.attributes.checked=false;
                                parentNode.ui.toggleCheck();
                            }
                            return ;
                        }
                    }
                    parentNode.attributes.checked=true;
                    parentNode.ui.toggleCheck();
                }
            }else{
                /**
                 * 如果为false时，
                 */
                if(parentNode&&parentNode.attributes.checked){
                    parentNode.attributes.checked=false;
                    parentNode.ui.toggleCheck();
                }
            }
        
        }
    });   
})